﻿using Cofe.Core;
using Cofe.Core.Interfaces;
using Cofe.Core.Property;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#if NETFX_CORE
using Windows.Storage;
using Cofe.Core.Logger;
#endif

namespace Cofe.IO.Implements
{
#if NETFX_CORE
    public class WindowsStorageDirectoryLister : IDirectoryLister
    {
        #region Constructor
        public WindowsStorageDirectoryLister(IStorageFolder storageFolder)
        {
            ManualEventSource = new DummyCofeSystemEventSource();
            _storageFolder = storageFolder;
        }

        #endregion

        #region Methods

        private async Task<IStorageItem> lookup(IStorageFolder _parentFolder, string[] pathSplit)
        {
            if (pathSplit.Length == 0)
                return _parentFolder;

            if (pathSplit.Length == 1 && string.IsNullOrEmpty(pathSplit[0]))
                return _parentFolder;

            IStorageFolder _lookupFolder = _parentFolder;
            for (int id = 0; id < pathSplit.Length - 1; id++)
            {
                try
                {
                    _lookupFolder = await _lookupFolder.GetFolderAsync(pathSplit[id]);
                    if (_lookupFolder == null)
                        return null; //Found failed.
                }
                catch
                {
                    return null;
                }
            }

            return await _lookupFolder.GetItemAsync(pathSplit[pathSplit.Length - 1]);
        }


        public async Task<IPropertyProvider> TryParsePathAsync(string path, EntryConstructionFlags flags)
        {
            IPropertyProvider parsedEntryCore = null;

            string fullPath = PathFE.Combine(VolumePath, path);
            var foundItem = await lookup(_storageFolder, path.Split('\\'));
            if (foundItem != null)
                parsedEntryCore = new WindowsStoragePropertyProvider(this, foundItem) 
                { ManualEventSource = ManualEventSource };

            if (parsedEntryCore != null)
                CofeServices.Logger.Log("WindowsStorage parsed {0}", path);

            return parsedEntryCore;
        }


      public IEnumerable<IPropertyProvider> GetRootPropertyProviders()
        {
            var rootItem = lookup(_storageFolder, new string[] { }).Result;
            yield return new WindowsStoragePropertyProvider(this,
                             rootItem) { ManualEventSource = ManualEventSource };
        }

        #endregion

    #region Data

        private IVolume _volume;
        private IStorageFolder _storageFolder;

    #endregion

    #region Public Properties


        public IManualCofeSystemEventSource ManualEventSource { get; set; }

        public string VolumePath
        {
            get { return Volume.Parameters["VolumePath"]; }
        }

        public IVolume Volume
        {
            get { return _volume; }
            set { _volume = value; }
        }

    #endregion
    }
#endif
}
